/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.service.subtitle;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.source.dao.subtitle.EleSourceSubtitleDao;
import com.thinkgem.jeesite.modules.source.dao.subtitle.EleSourceSubtitleGroupDao;
import com.thinkgem.jeesite.modules.source.entity.subtitle.EleSourceSubtitle;
import com.thinkgem.jeesite.modules.source.entity.subtitle.EleSourceSubtitleGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 字幕资源管理Service
 * @author ZC
 * @version 2017-04-22
 */
@Service
@Transactional(readOnly = true)
public class EleSourceSubtitleGroupService extends CrudService<EleSourceSubtitleGroupDao, EleSourceSubtitleGroup> {

	@Autowired
	private EleSourceSubtitleDao eleSourceSubtitleDao;
	
	public EleSourceSubtitleGroup get(String id) {
		EleSourceSubtitleGroup eleSourceSubtitleGroup = super.get(id);
		eleSourceSubtitleGroup.setEleSourceSubtitleList(eleSourceSubtitleDao.findList(new EleSourceSubtitle(eleSourceSubtitleGroup)));
		return eleSourceSubtitleGroup;
	}
	
	public List<EleSourceSubtitleGroup> findList(EleSourceSubtitleGroup eleSourceSubtitleGroup) {
		return super.findList(eleSourceSubtitleGroup);
	}
	
	public Page<EleSourceSubtitleGroup> findPage(Page<EleSourceSubtitleGroup> page, EleSourceSubtitleGroup eleSourceSubtitleGroup) {
		return super.findPage(page, eleSourceSubtitleGroup);
	}
	
	@Transactional(readOnly = false)
	public void save(EleSourceSubtitleGroup eleSourceSubtitleGroup) {
		try{
			super.save(eleSourceSubtitleGroup);
			for(EleSourceSubtitle eleSourceSubtitle : eleSourceSubtitleGroup.getEleSourceSubtitleList()){
				if(eleSourceSubtitle.getId() == null){
					continue;
				}
				if(EleSourceSubtitle.DEL_FLAG_NORMAL.equals(eleSourceSubtitle.getDelFlag())){
					if(StringUtils.isBlank(eleSourceSubtitle.getId())){
						eleSourceSubtitle.setGroup(eleSourceSubtitleGroup);
						eleSourceSubtitle.preInsert();
						eleSourceSubtitleDao.insert(eleSourceSubtitle);
					}
					else{
						eleSourceSubtitle.preUpdate();
						eleSourceSubtitleDao.update(eleSourceSubtitle);
					}
				}
				else{
					eleSourceSubtitleDao.delete(eleSourceSubtitle);
				}
			}
		}catch(RuntimeException e){
			logger.error("新建字幕资源失败!");
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(EleSourceSubtitleGroup eleSourceSubtitleGroup) {
		super.delete(eleSourceSubtitleGroup);
		eleSourceSubtitleDao.delete(new EleSourceSubtitle(eleSourceSubtitleGroup));
	}
	
}